package com.xunyin.modules.mtuserbalance.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.modules.mtuserbalance.domain.MtUserBalance;
import com.xunyin.modules.mtuserbalance.service.MtUserBalanceService;
import com.xunyin.modules.mtuserbalance.service.dto.UserBalanceStatDto;
import com.xunyin.modules.mtuserbalance.service.mapper.MtUserBalanceMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 用户门店余额实现类
 * <p>
 * Created by xunyin
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Service
@AllArgsConstructor
@Slf4j
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MtUserBalanceServiceImpl extends BaseServiceImpl<MtUserBalanceMapper, MtUserBalance> implements MtUserBalanceService {

    @Autowired
    private MtUserBalanceMapper mtUserBalanceMapper;

    @Override
    public List<MtUserBalance> queryMtUserBalanceByParams(Map<String, Object> params) {
        LambdaQueryWrapper<MtUserBalance> lambdaQueryWrapper = Wrappers.lambdaQuery();
        Integer storeId = params.get("storeId") == null ? 0 : Integer.valueOf(params.get("storeId").toString());
        if (storeId > 0) {
            lambdaQueryWrapper.eq(MtUserBalance::getStoreId, storeId);
        }
        Integer userId = params.get("userId") == null ? 0 : Integer.valueOf(params.get("userId").toString());
        if (userId > 0) {
            lambdaQueryWrapper.eq(MtUserBalance::getUserId, userId);
        }

        // 根据门店Id与当前登录用户Id-获取用户在对应门店的余额
        List<MtUserBalance> dataList = mtUserBalanceMapper.selectList(lambdaQueryWrapper);
        if (storeId > 0 && userId > 0) {

            // 如果没用户门店余额记录，则添加一条
            if (dataList == null || dataList.size() < 1) {
                MtUserBalance mtUserBalance = new MtUserBalance();
                mtUserBalance.setUserBalance(new BigDecimal(0));
                mtUserBalance.setUserId(userId);
                mtUserBalance.setStoreId(storeId);
                mtUserBalanceMapper.insert(mtUserBalance);
                dataList = mtUserBalanceMapper.selectList(lambdaQueryWrapper);
            }
        }

        return dataList;
    }

    /**
     * 获取用户充值统计数据
     * @return userBalanceStatDto
     */
    @Override
    public UserBalanceStatDto getUserBalanceStat(Integer storeId, String storeIds, String mobile) {

        UserBalanceStatDto userBalanceStatDto = new UserBalanceStatDto();

//        if (storeId == 0) {
//            storeId = null;
//        }

        if("".equals(mobile)){
            mobile = null;
        }

        log.info("getUserBalanceStat-storeIds:"+storeIds);

        log.info("getUserBalanceStat-mobile:"+mobile);

        userBalanceStatDto.setSumUserBalanceMoney(mtUserBalanceMapper.sumUserBalanceMoney(storeId, storeIds, mobile));

        return userBalanceStatDto;

    }

}
